package com.asr.testclient.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 测试AudioFileHandler类
 */
class AudioFileHandlerTest {
    
    private AudioFileHandler audioFileHandler;
    
    @TempDir
    Path tempDir;
    
    @BeforeEach
    void setUp() {
        audioFileHandler = new AudioFileHandler();
    }
    
    @Test
    void testEncodeToBase64_WithValidData() {
        // 准备测试数据
        byte[] testData = "Hello, ASR Server!".getBytes();
        
        // 执行编码
        String encoded = audioFileHandler.encodeToBase64(testData);
        
        // 验证结果
        assertNotNull(encoded);
        assertFalse(encoded.isEmpty());
        
        // 验证编码正确性 - 解码后应该与原始数据相同
        byte[] decoded = Base64.getDecoder().decode(encoded);
        assertArrayEquals(testData, decoded);
    }
    
    @Test
    void testEncodeToBase64_WithEmptyData() {
        byte[] emptyData = new byte[0];
        
        String encoded = audioFileHandler.encodeToBase64(emptyData);
        
        // 空数据应该返回空字符串
        assertEquals("", encoded);
    }
    
    @Test
    void testEncodeToBase64_WithNullData() {
        String encoded = audioFileHandler.encodeToBase64(null);
        
        // null数据应该返回空字符串
        assertEquals("", encoded);
    }
    
    @Test
    void testValidateFileSize_WithValidFile() throws IOException {
        // 创建一个小于10MB的测试文件
        File testFile = tempDir.resolve("test_audio.wav").toFile();
        try (FileOutputStream fos = new FileOutputStream(testFile)) {
            byte[] data = new byte[1024]; // 1KB
            fos.write(data);
        }
        
        boolean isValid = audioFileHandler.validateFileSize(testFile);
        
        assertTrue(isValid);
    }
    
    @Test
    void testValidateFileSize_WithOversizedFile() throws IOException {
        // 创建一个超过10MB的测试文件
        File testFile = tempDir.resolve("large_audio.wav").toFile();
        try (FileOutputStream fos = new FileOutputStream(testFile)) {
            byte[] data = new byte[1024 * 1024]; // 1MB块
            for (int i = 0; i < 11; i++) { // 写入11MB
                fos.write(data);
            }
        }
        
        boolean isValid = audioFileHandler.validateFileSize(testFile);
        
        assertFalse(isValid);
    }
    
    @Test
    void testValidateFileSize_WithNullFile() {
        boolean isValid = audioFileHandler.validateFileSize(null);
        
        assertFalse(isValid);
    }
    
    @Test
    void testReadAudioFile_WithValidFile() throws IOException {
        // 创建测试文件
        File testFile = tempDir.resolve("test_audio.wav").toFile();
        byte[] expectedData = "Test audio data content".getBytes();
        try (FileOutputStream fos = new FileOutputStream(testFile)) {
            fos.write(expectedData);
        }
        
        // 读取文件
        byte[] actualData = audioFileHandler.readAudioFile(testFile);
        
        // 验证读取的数据正确
        assertNotNull(actualData);
        assertArrayEquals(expectedData, actualData);
    }
    
    @Test
    void testReadAudioFile_WithNonExistentFile() {
        File nonExistentFile = new File(tempDir.toFile(), "non_existent.wav");
        
        // 应该抛出IOException
        IOException exception = assertThrows(IOException.class, () -> {
            audioFileHandler.readAudioFile(nonExistentFile);
        });
        
        assertTrue(exception.getMessage().contains("不存在"));
    }
    
    @Test
    void testReadAudioFile_WithNullFile() {
        // 应该抛出IOException
        IOException exception = assertThrows(IOException.class, () -> {
            audioFileHandler.readAudioFile(null);
        });
        
        assertTrue(exception.getMessage().contains("为空"));
    }
    
    @Test
    void testReadAudioFile_WithEmptyFile() throws IOException {
        // 创建空文件
        File emptyFile = tempDir.resolve("empty.wav").toFile();
        emptyFile.createNewFile();
        
        // 应该抛出IOException
        IOException exception = assertThrows(IOException.class, () -> {
            audioFileHandler.readAudioFile(emptyFile);
        });
        
        assertTrue(exception.getMessage().contains("为空"));
    }
    
    @Test
    void testValidateFileFormat_WithWavFile() throws IOException {
        File wavFile = tempDir.resolve("test.wav").toFile();
        wavFile.createNewFile();
        
        boolean isValid = audioFileHandler.validateFileFormat(wavFile);
        
        assertTrue(isValid);
    }
    
    @Test
    void testValidateFileFormat_WithPcmFile() throws IOException {
        File pcmFile = tempDir.resolve("test.pcm").toFile();
        pcmFile.createNewFile();
        
        boolean isValid = audioFileHandler.validateFileFormat(pcmFile);
        
        assertTrue(isValid);
    }
    
    @Test
    void testValidateFileFormat_WithInvalidFile() throws IOException {
        File invalidFile = tempDir.resolve("test.mp3").toFile();
        invalidFile.createNewFile();
        
        boolean isValid = audioFileHandler.validateFileFormat(invalidFile);
        
        assertFalse(isValid);
    }
    
    @Test
    void testValidateFileFormat_WithNullFile() {
        boolean isValid = audioFileHandler.validateFileFormat(null);
        
        assertFalse(isValid);
    }
}
